unit uProducao;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, FMTBcd, DB, DBClient, SqlExpr, Provider, Grids,
  DBGrids, JvExDBGrids, JvDBGrid, StdCtrls, Buttons, ACBrBase, ACBrETQ ,ACBrDevice;

type
  TfProducao = class(TForm)
    sqlDetOf: TSQLDataSet;
    dspDetOf: TDataSetProvider;
    cdsDetOf: TClientDataSet;
    cdsDetOfDOF_CODIGO: TFMTBCDField;
    cdsDetOfDOF_CODIOF: TStringField;
    cdsDetOfDOF_QTDPRG: TFMTBCDField;
    cdsDetOfDOF_STATUS: TStringField;
    dsDetOf: TDataSource;
    Panel1: TPanel;
    gridProd: TJvDBGrid;
    BitBtn1: TBitBtn;
    cdsDetOfPRO_QTDEMB: TFMTBCDField;
    BitBtn7: TBitBtn;
    ACBrETQ: TACBrETQ;
    Label1: TLabel;
    Edit1: TEdit;
    Label2: TLabel;
    Edit2: TEdit;
    BitBtn5: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure gridProdCellClick(Column: TColumn);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fProducao: TfProducao;

implementation

uses udm;

{$R *.dfm}

procedure TfProducao.BitBtn1Click(Sender: TObject);
var strProd: String;
begin
  // Abre o Grid com as OFs de Producao (OF Desmembrada)
  if (cdsDetOf.Active) then
    cdsDetOf.Close;
  cdsDetOf.Open;

end;

procedure TfProducao.BitBtn7Click(Sender: TObject);
var ordem , seg, strA : string;
    i, numero, multiplo  : integer;
    h : double;
    n : integer;
begin
  n := 2;
  if  MessageDlg('Imprimir Etiquetas OF ' +
    QuotedStr(cdsDetOfDOF_CODIOF.AsString + '/' + IntToStr(cdsDetOfDOF_CODIGO.AsInteger)),
    mtConfirmation, [mbYes, mbNo],0) = mrNo then exit;

  strA := 'SELECT  ETI_CODETI, ETI_QTDENV, ETI_CODPRO, ETI_LOTE,  DOF_CODIOF, ' +
    ' DOF_CODIGO, ETI_DATETI , substr(PRO_DESCRI, 0,37) PRO_DESCRI  , ' +
    ' substr(PRO_DESCRI, 38,37) PRO_DESCRI1, DOF_DATDOF, DOF_CODTUR ' +
    ' FROM F_ETIQUETAS, F_PRODS, ATS_DETOF ' +
    ' WHERE ETI_CODPRO = PRO_CODPRO ' +
    ' AND ETI_COMP19 = DOF_CODIGO ' +
    ' AND DOF_CODIGO = :pOF ' +
    ' AND ETI_IMPRES = 0 ' +
    ' AND ETI_CODETI BETWEEN ' + QuotedStr(Edit1.Text) + ' AND ' + QuotedStr(Edit2.Text);

  if (dm.cdsEtiqueta.Active) then
    dm.cdsEtiqueta.close;

  dm.cdsEtiqueta.CommandText := strA;
  dm.cdsEtiqueta.Params.ParamByName('pOF').AsString := cdsDetOfDOF_CODIGO.AsString;

  dm.cdsEtiqueta.open;

  if (dm.cdsEtiqueta.IsEmpty) then
  begin
    MessageDlg('Sem Etiquetas para imprimir.', mtWarning, [mbYes, mbNo],0);
    exit;
  end;

  i := 1;
  with ACBrETQ do
  begin
    Modelo := TACBrETQModelo(1);
    Porta := 'Lpt1';
    Avanco := 600 ;
    Ativar;
    Temperatura := 12;
    while not dm.cdsEtiqueta.EOF do
    begin
      strA := 'UPDATE F_ETIQUETAS SET ETI_IMPRES = 1 WHERE ETI_CODETI = ' + QuotedStr(dm.cdsEtiquetaETI_CODETI.AsString);

      ordem := dm.cdsEtiquetaDOF_CODIOF.AsString;
      numero := i;
      //ImprimirTexto(orNormal, 2, n, n, 550, 0050,( 'OF' + ' ' + ordem ));
      ImprimirCaixa(10,10,160,230,1,1);
      ImprimirTexto(or90, 0, n, n, 050, 080,( 'Prod:' + ' ' + dm.cdsEtiquetaETI_CODPRO.AsString ));
      ImprimirTexto(or90, 0, n, n, 370, 080,( DateTostr(dm.cdsEtiquetaDOF_DATDOF.AsDateTime) + '-T' + IntToStr(dm.cdsEtiquetaDOF_CODTUR.AsInteger)));
      ImprimirTexto(or90, 0, n, n, 050, 120,(dm.cdsEtiquetaPRO_DESCRI.AsString ));
      ImprimirTexto(or90, 0, n, n, 050, 130,(dm.cdsEtiquetaPRO_DESCRI1.AsString ));
      ImprimirLinha(10,70,1,230);  // Linha Horizontal
      // OF
      ImprimirTexto(or90, 0, n, n, 055, 210,( 'O.F.:'));
      //ImprimirLinha(left, top, altura, espessura)
      ImprimirLinha(120,70,45, 1);  // Linha Vertical
      //ImprimirLinha(120,40,50, 1);  // Linha Vertical
      ImprimirTexto(or90, 0, n, n, 380, 210,( 'Quantidade:'));
      ImprimirTexto(or90, 0, n, n, 055, 280,( dm.cdsEtiquetaDOF_CODIOF.AsString + '/' +
        IntToStr(dm.cdsEtiquetaDOF_CODIGO.AsInteger)));
      ImprimirTexto(or90, 1, n, n, 380, 280,( FloatToStr(dm.cdsEtiquetaETI_QTDENV.AsFloat)));
      //ImprimirLinha(left,top,espessura,comprimento);  // Linha Horizontal
      ImprimirLinha(10,115,1,230);  // Linha Horizontal
      //ImprimirTexto(or90, 2, n, n, 100, 0200, IntToStr(numero));
      //ImprimirBarras(orNormal, 'E', '2', '2', 250, 50, 'E79970044Q', 90);  // Esta imprime correto  PERFEITO
      ImprimirBarras(or90, 'E', '2', '2', 130, 430, dm.cdsEtiquetaETI_CODETI.AsString, 110);  // Esta imprime correto  PERFEITO
      try
        dm.SQLConnection1.ExecuteDirect(strA);
      except
        MessageDlg('Erro na gravacao Etiqueta : ' + QuotedStr(dm.cdsEtiquetaETI_CODETI.AsString),
          mtError, [mbYes, mbNo],0);
        exit;
      end;

      dm.cdsEtiqueta.Next;


      // ETIQUETA 2
      strA := 'UPDATE F_ETIQUETAS SET ETI_IMPRES = 1 WHERE ETI_CODETI = ' + QuotedStr(dm.cdsEtiquetaETI_CODETI.AsString);

      //ImprimirTexto(orNormal, 2, n, n, 550, 0050,( 'OF' + ' ' + ordem ));
      ImprimirCaixa(10,210,160,230,1,1);
      ImprimirTexto(or90, 0, n, n, 050, 580,( 'Prod:' + ' ' + dm.cdsEtiquetaETI_CODPRO.AsString ));
      ImprimirTexto(or90, 0, n, n, 370, 580,(DateTostr(dm.cdsEtiquetaDOF_DATDOF.AsDateTime) + '-T' + IntToStr(dm.cdsEtiquetaDOF_CODTUR.AsInteger)));
      ImprimirTexto(or90, 0, n, n, 050, 610,(dm.cdsEtiquetaPRO_DESCRI.AsString ));
      ImprimirTexto(or90, 0, n, n, 050, 630,(dm.cdsEtiquetaPRO_DESCRI1.AsString ));
      // OF
      ImprimirLinha(10,260,1,230);  // Linha Horizontal
      ImprimirTexto(or90, 0, n, n, 055, 700,( 'O.F.:'));
      ImprimirTexto(or90, 0, n, n, 380, 700,( 'Quantidade:'));
      ImprimirLinha(120,260,50, 1);  // Linha Vertical
      ImprimirLinha(10,310,1,230);  // Linha Horizontal
      ImprimirTexto(or90, 0, n, n, 055, 770,( dm.cdsEtiquetaDOF_CODIOF.AsString + '/' +
        IntToStr(dm.cdsEtiquetaDOF_CODIGO.AsInteger) ));
      ImprimirTexto(or90, 1, n, n, 380, 770,( FloatToStr(dm.cdsEtiquetaETI_QTDENV.AsFloat)));

      ImprimirBarras(or90, 'E', '2', '2', 130, 930, dm.cdsEtiquetaETI_CODETI.AsString, 110);

      Imprimir(StrToInt('1')); // Numero de Copias
      try
        dm.SQLConnection1.ExecuteDirect(strA);
      except
        MessageDlg('Erro na gravacao Etiqueta : ' + QuotedStr(dm.cdsEtiquetaETI_CODETI.AsString),
          mtError, [mbYes, mbNo],0);
        exit;
      end;

      //Evitando o estouro do Buffer da impressora.
      multiplo :=  i mod 5;
      if multiplo = 0 then
      begin
        //cont := 0;
        Sleep(1500);
        Application.ProcessMessages;
      end;
      i := i + 2;
      dm.cdsEtiqueta.Next;
    end;
    Desativar;
  end;
end;

procedure TfProducao.gridProdCellClick(Column: TColumn);
var str: string;
begin
  str := 'SELECT  ETI_CODETI, ETI_QTDENV, ETI_CODPRO, ETI_LOTE,  DOF_CODIOF, ' +
    ' DOF_CODIGO, ETI_DATETI , substr(PRO_DESCRI, 0,37) PRO_DESCRI  , ' +
    ' substr(PRO_DESCRI, 38,37) PRO_DESCRI1, DOF_DATDOF, DOF_CODTUR ' +
    ' FROM F_ETIQUETAS, F_PRODS, ATS_DETOF ' +
    ' WHERE ETI_CODPRO = PRO_CODPRO ' +
    ' AND ETI_COMP19 = DOF_CODIGO ' +
    ' AND DOF_CODIGO = :pOF ' +
    ' AND ETI_IMPRES = 0 ' ;

  if (dm.cdsEtiqueta.Active) then
    dm.cdsEtiqueta.close;
  dm.cdsEtiqueta.CommandText := str;
  dm.cdsEtiqueta.Params.ParamByName('pOF').AsString := cdsDetOfDOF_CODIGO.AsString;
  dm.cdsEtiqueta.Open;
  dm.cdsEtiqueta.First;
  Edit1.Text := dm.cdsEtiquetaETI_CODETI.AsString;
  dm.cdsEtiqueta.Last;
  Edit2.Text := dm.cdsEtiquetaETI_CODETI.AsString;
end;

end.
